﻿// =====================================================================
// <copyright file="TextHelper.cs" author="David R. Guindo">
// 	Copyright (C) 2013. All rights reserved.
// </copyright>
// =====================================================================
// This code is released under the terms of the Apache 2.0 license, 
// http://apache.org/licenses/LICENSE-2.0.html
// =====================================================================

namespace AppBlock.Core
{
    using System.Collections.Generic;
    using System.Data.Entity.Design.PluralizationServices;
    using System.Globalization;
    using System.Text.RegularExpressions;

    /// <summary>
    ///     Helper for strings processing operations.
    /// </summary>
    public static class TextHelper
    {
        /// <summary>
        ///     Truncates the specified value string stracting blocks by separator
        ///     an join the specified blocks.
        /// </summary>
        /// <param name="value"> The value. </param>
        /// <param name="blocks"> The blocks. </param>
        /// <param name="separator"> The separator. </param>
        /// <returns> </returns>
        public static string Truncate(string value, int blocks, char separator = '.')
        {
            string[] textBlocks = value.Split(separator);
            return Truncate(textBlocks, blocks, separator);
        }

        /// <summary>
        ///     Truncates the specified ?.
        /// </summary>
        /// <param name="textBlocks"> The text blocks. </param>
        /// <param name="blocks"> The blocks. </param>
        /// <param name="separator"> The separator. </param>
        /// <returns> </returns>
        public static string Truncate(IList<string> textBlocks, int blocks, char separator = '.')
        {
            if (blocks > textBlocks.Count)
            {
                blocks = textBlocks.Count;
            }

            string returnValue = "";
            for (int i = 0; i < blocks - 1; i++)
            {
                returnValue += textBlocks[i] + separator;
            }

            returnValue += textBlocks[blocks - 1];

            return returnValue;
        }

        /// <summary>
        ///     Pluralizes the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>System.String.</returns>
        public static string Pluralize(string name, string culture = "en-US")
        {
            return PluralizationService.CreateService(new CultureInfo(culture)).Pluralize(name);
        }

        /// <summary>
        ///     Converts a wildcard to a regex.
        /// </summary>
        /// <param name="pattern">The wildcard pattern to convert.</param>
        /// <returns>A regex equivalent of the given wildcard.</returns>
        public static string WildcardToRegex(string pattern)
        {
            return "^" + Regex.Escape(pattern).
                Replace("\\*", ".*").
                Replace("\\?", ".") + "$";
        }
    }
}